/**
 * 深度优先遍历，使用邻接矩阵表示
 * 
 */
#include<stdio.h>
#include<stdlib.h>
#define max_vertex_num 100
typedef struct 
{
    int vertex_num,edge_num; //顶点数和边数
    int visited[max_vertex_num];//顶点访问标记
    int edge[max_vertex_num][max_vertex_num];//临界矩阵
    
}Graph;

/**
 * 访问i节点，并标记
 * @return int 如果已经访问过，就返回0，没有访问过就访问，并返回1
 */
int visit(Graph *g,int i){
    printf("访问节点%d \n",i);
    if(g->visited[i]==0){
        g->visited[i]=1;
        return 1;
    }else{
        return 0;
    }
}

void dfs(Graph *g,int i){
    visit(g,i);
    for(int j=0;j<g->vertex_num;j++){//遍历当前行，只要有边到其他节点，就往下访问
        if(g->visited[j]==0&&g->edge[i][j]==1){
            dfs(g,j);
        }
    
}

int main(void){
    //测试数据,正确结果为0，1，2，3，4，5，6，7
    Graph g1;
    g1.edge_num=7;
    g1.vertex_num=8;
    //王道228页
    int data[][8]={
        {0,1,1,0,0,0,0,0},
        {1,0,0,1,1,0,0,0},
        {1,0,0,0,0,1,1,0},
        {0,1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,1},
        {0,0,1,0,0,0,0,0},
        {0,0,1,0,0,0,0,0},
        {0,0,0,0,1,0,0,0},
    };
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            g1.edge[i][j]=data[i][j];
        }
    }
    dfs(&g1,0);
    return 0;
}